from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from page.base_page_helin import BasePage

class AddgoodsPage(BasePage):
    # 进入添加产品页面定位器
    cp_locator=(By.LINK_TEXT,'产品')
    tjcp_locator = (By.LINK_TEXT, '添加产品')

    # 添加产品定位器
    goodsnm_locator = (By.ID, 'name')
    goodslb_locator = (By.ID, "category_id")
    goodslj_locator = (By.ID, "link")
    goodstd_locator = (By.ID, "development_team")
    goodscbj_locator = (By.ID, "cost_price")
    goodsjysj_locator = (By.ID, "suggested_price")
    goodszt_locator = (By.ID, "main_pic")
    goodsbz_locator = (By.ID, "description")
    qd_locator = (By.CSS_SELECTOR, "input[value=保存]")

    # 清除数据定位器
    qcsj_locator = (By.XPATH, '//*[@id="form1"]/table/tbody')
    delete_locator = (By.ID, 'delete')
    delete_goods_data_locator=(By.CSS_SELECTOR, "#form1 > table > tbody > tr:nth-child(1) > td:nth-child(3) > a")
    choice_goods_locator=(By.CSS_SELECTOR, 'input[name="product_id[]"]')

    # 打开浏览器(from base_case)
    # 登录进入首页(from base_case)

    # 进入添加产品页面
    def click_goods(self):
        self.driver.find_element(*self.cp_locator).click()

    def click_addgoods(self):
        self.driver.find_element(*self.tjcp_locator).click()

    # 添加产品
    def add_goodsnm(self,goodsnm):
        self.driver.find_element(*self.goodsnm_locator).send_keys(goodsnm)

    def add_goodslb(self, goodslb):
        self.lb_element = self.driver.find_element(*self.goodslb_locator)
        select = Select(self.lb_element)
        select.select_by_visible_text(goodslb)

    def add_goodslj(self, goodslj):
        self.driver.find_element(*self.goodslj_locator).send_keys(goodslj)

    def add_goodstd(self, goodstd):
        self.driver.find_element(*self.goodstd_locator).send_keys(goodstd)

    def add_goodscbj(self, goodscbj):
        self.driver.find_element(*self.goodscbj_locator).send_keys(goodscbj)

    def add_goodsjysj(self, goodsjysj):
        self.driver.find_element(*self.goodsjysj_locator).send_keys(goodsjysj)

    def add_goodszt(self, goodszt):
        self.driver.find_element(*self.goodszt_locator).send_keys(goodszt)

    def add_goodsbz(self, goodsbz):
        self.driver.find_element(*self.goodsbz_locator).send_keys(goodsbz)

    def add_goodsqd(self):
        self.driver.find_element(*self.qd_locator).click()

    # 清除数据
    def clear_data(self,goodsnm):
        tbody = self.driver.find_element(*self.qcsj_locator)
        trs = tbody.find_elements(By.TAG_NAME, "tr")
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, 'td')
            cp = tds[2].find_element(*self.delete_goods_data_locator).text
            if cp == goodsnm:
                tds[0].find_element(*self.choice_goods_locator).click()
                break
            self.driver.find_element(*self.delete_locator).click()







